package com.ibeeking.found.msg.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.msg.rest.service.ISysMsgRecordReceiverService;
import com.ibeeking.found.msg.service.common.dos.SysMsgRecordDO;
import com.ibeeking.found.msg.service.common.dos.SysMsgRecordReceiverDO;
import com.ibeeking.found.msg.service.common.vo.SysMsgRecordVO;
import com.ibeeking.found.msg.service.mapper.SysMsgRecordMapper;
import com.ibeeking.found.msg.service.mapper.SysMsgRecordReceiverMapper;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SysMsgRecordReceiverServiceImpl
 * description 系统消息记录
 * @Author ibeeking
 * @Date 2020-12-09 10:54
 */
@Service
public class SysMsgRecordReceiverServiceImpl extends ServiceImpl<SysMsgRecordReceiverMapper, SysMsgRecordReceiverDO> implements ISysMsgRecordReceiverService {

    @Resource
    private SysMsgRecordMapper sysMsgRecordMapper;

    @Resource
    private SysMsgRecordReceiverMapper sysMsgRecordReceiverMapper;

    @Override
    public List<SysMsgRecordVO> listByTenantAndReceiverId(Long tenantId, Long receiverId) {
        List<SysMsgRecordReceiverDO> sysMsgRecordReceiverDOS = sysMsgRecordReceiverMapper.selectList(new LambdaQueryWrapper<SysMsgRecordReceiverDO>()
                .eq(SysMsgRecordReceiverDO::getTenantId,tenantId).eq(SysMsgRecordReceiverDO::getReceiver,receiverId));
        if(Collections3.isEmpty(sysMsgRecordReceiverDOS)) return Collections3.emptyList();
        List<Long> recordIds = sysMsgRecordReceiverDOS.stream().map(SysMsgRecordReceiverDO::getId).collect(Collectors.toList());
        List<SysMsgRecordDO> sysMsgRecordDOS = sysMsgRecordMapper.selectList(new LambdaQueryWrapper<SysMsgRecordDO>().in(SysMsgRecordDO::getId,recordIds));
        if(Collections3.isEmpty(sysMsgRecordDOS)) return Collections3.emptyList();
        return JsonUtils.jsonToList(sysMsgRecordDOS, SysMsgRecordVO.class);
    }

    @Override
    public List<SysMsgRecordVO> listUnReadByTenantAndReceiverId(Long tenantId, Long receiverId) {
        List<SysMsgRecordReceiverDO> sysMsgRecordReceiverDOS = sysMsgRecordReceiverMapper.selectList(new LambdaQueryWrapper<SysMsgRecordReceiverDO>()
                .eq(SysMsgRecordReceiverDO::getTenantId,tenantId).eq(SysMsgRecordReceiverDO::getReceiver,receiverId).eq(SysMsgRecordReceiverDO::getReadStatus, 0));
        if(Collections3.isEmpty(sysMsgRecordReceiverDOS)) return Collections3.emptyList();

        List<Long> recordIds = sysMsgRecordReceiverDOS.stream().map(SysMsgRecordReceiverDO::getRecordId).collect(Collectors.toList());
        List<SysMsgRecordDO> sysMsgRecordDOS = sysMsgRecordMapper.selectList(new LambdaQueryWrapper<SysMsgRecordDO>().in(SysMsgRecordDO::getId,recordIds));
        if(Collections3.isEmpty(sysMsgRecordDOS)) return Collections3.emptyList();

        return JsonUtils.jsonToList(sysMsgRecordDOS, SysMsgRecordVO.class);
    }

    @Override
    public Integer unReadCount(Long tenantId, Long receiverId){
        return super.count(new LambdaQueryWrapper<SysMsgRecordReceiverDO>()
                .eq(SysMsgRecordReceiverDO::getTenantId,tenantId).eq(SysMsgRecordReceiverDO::getReceiver,receiverId).eq(SysMsgRecordReceiverDO::getReadStatus, 0));
    }

    @Override
    public SysMsgRecordVO viewSysMsgRecord(Long id) {
        SysMsgRecordDO sysMsgRecordDO = sysMsgRecordMapper.selectById(id);
        if(sysMsgRecordDO == null) return null;
        return BeanUtil.convertBean(sysMsgRecordDO, SysMsgRecordVO.class);
    }

    @Override
    public Boolean changeReadStatus(Long id, Integer status) {
        List<SysMsgRecordReceiverDO> sysMsgRecordReceiverDOS = sysMsgRecordReceiverMapper.selectList(new LambdaQueryWrapper<SysMsgRecordReceiverDO>()
                .eq(SysMsgRecordReceiverDO::getRecordId,id));;
        if(Collections3.isEmpty(sysMsgRecordReceiverDOS)) return true;
        sysMsgRecordReceiverDOS.stream().forEach(d->{
            d.setReadStatus(1);
        });
        return super.updateBatchById(sysMsgRecordReceiverDOS);
    }
}
